package simtester.server

//import static org.junit.Assert.*;

import grails.test.mixin.TestFor
import org.junit.Before
import org.junit.BeforeClass
import org.junit.Test

@TestFor(TestCase)
class TestCaseTests {
    static testuser
    
    @BeforeClass
    static void setUpClass() {
        testuser = new User(username: 'testuser', password: 'pass')
    }

    @Test
    void createTestCaseSuccessfully() {
        def team = new Team(name: 'svt')
        def tc = new TestCase(name: 'MyTestCase', script: 'my script', type: 'GUI', owner: testuser, team: team)
        assert tc.validate()
    }
    
    //@Test
    void createTestCaseThatAlreadyExists() {
        def team = new Team(name: 'svt')
        def existingTestCase = new TestCase(name: 'MyTestCase', script: 'my script', owner: testuser, team: team)
        
        mockForConstraintsTests(TestCase, [existingTestCase])
        
        def newTestCase = new TestCase(name: 'MyTestCase', script: 'some script', owner: testuser, team: team)
        assert !newTestCase.validate()
        assert newTestCase.errors['name'] == 'unique'
    }
    
    @Test
    void createTestCaseWithNameThatAlreadyExists() {
        def team = new Team(name: 'svt')
        def existingTestCase = new TestCase(name: 'MyTestCase', script: 'my script', type: 'GUI', owner: testuser, team: team)
        
        mockForConstraintsTests(TestCase, [existingTestCase])
        
        def newTestCase = new TestCase(name: 'MyTestCase', script: 'some script', type: 'GUI', owner: testuser, team: team)
        assert newTestCase.validate()
    }
    
    @Test
    void createUserWithoutRequiredFields() {
        def tc = new TestCase()
        assert !tc.validate()
        assert 'nullable' in tc.errors['name'].codes
        assert 'nullable' in tc.errors['script'].codes
        assert 'nullable' in tc.errors['owner'].codes
    }

}
